package com.shujia.flink.table

import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.table.api.EnvironmentSettings
import org.apache.flink.table.api.bridge.scala.StreamTableEnvironment

object Demo7DataGen {
  def main(args: Array[String]): Unit = {
    //创建flink 环境
    val bsEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    //设置table 环境的一些参数
    val bsSettings: EnvironmentSettings = EnvironmentSettings.newInstance()
      .useBlinkPlanner() //使用blikc计划器
      .inStreamingMode() //流模式
      .build()

    // 创建flink  table 环境
    val bsTableEnv: StreamTableEnvironment = StreamTableEnvironment.create(bsEnv, bsSettings)

    /**
      * datagen: 随机生成数据的工具
      *
      */
    bsTableEnv.executeSql(
      """
        |CREATE TABLE datagen (
        | f_sequence INT,
        | f_random INT,
        | f_random_str STRING
        |) WITH (
        | 'connector' = 'datagen',
        | 'rows-per-second'='5',
        | 'fields.f_sequence.kind'='sequence',
        | 'fields.f_sequence.start'='1',
        | 'fields.f_sequence.end'='1000',
        | 'fields.f_random.min'='1',
        | 'fields.f_random.max'='1000',
        | 'fields.f_random_str.length'='10'
        |)
      """.stripMargin)

    bsTableEnv.executeSql(
      """
        |CREATE TABLE print_table WITH ('connector' = 'print')
        |LIKE datagen (EXCLUDING ALL)
      """.stripMargin)

    bsTableEnv.executeSql(
      """
        |CREATE TABLE blackhole_table WITH ('connector' = 'blackhole')
        |LIKE datagen (EXCLUDING ALL)
        |
      """.stripMargin)

    bsTableEnv.executeSql(
      """
        |insert into blackhole_table
        |select * from datagen
        |
      """.stripMargin)

  }

}
